package com.highspeed.bdk.controller.logic;

import com.highspeed.bdk.MainApplication;
import com.highspeed.bdk.bean.entity.logic.App;
import com.highspeed.bdk.bean.entity.logic.LogicLink;
import com.highspeed.bdk.bean.entity.logic.LogicTopology;
import org.apache.struts2.json.JSONUtil;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: bdk-view
 * @description:
 * @author: Chenjiabin
 * @create: 2018-04-22 20:29
 **/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {MainApplication.class})
@AutoConfigureMockMvc
@Transactional
@WithMockUser(username = "admin", roles = {"USER", "ADMIN", "abc"})
public class LogicTopologyControllerTester extends BaseAppServiceTester {
    @Autowired
    private MockMvc mvc;
    private LogicTopology logicTopology;

    @Before
    public void init() {
        LogicTopology pt = createLogicTopology();

        for (int a = 0; a < 5; a++) {
            App app = createApp();
            pt.getPreinstalledApps().add(app);
        }

        pt.getApps().add(pt.getPreinstalledApps().get(0));
        pt.getApps().add(pt.getPreinstalledApps().get(1));
        pt.getApps().add(pt.getPreinstalledApps().get(2));

        this.logicTopology = logicTopologyService.save(pt);
        LogicLink link = createLogicLink(logicTopology.getApps().get(0), logicTopology.getApps().get(1));
        logicLinkService.save(link);

        logicTopologyService.flush();
        logicTopologyService.clear();
    }

    @Test
    public void save() throws Exception {
        LogicTopology pt = createLogicTopology();

        for (int a = 0; a < 5; a++) {
            App app = createApp();
            pt.getPreinstalledApps().add(app);
        }

        pt.getApps().add(pt.getPreinstalledApps().get(0));
        pt.getApps().add(pt.getPreinstalledApps().get(1));
        pt.getApps().add(pt.getPreinstalledApps().get(2));


        String str = JSONUtil.serialize(pt);


        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/logic_topology/save");
        //希望服务器返回的格式
        builder.accept(MediaType.APPLICATION_JSON_UTF8);
        //发送数据格式
        builder.contentType(MediaType.APPLICATION_JSON_UTF8);
        builder.content(str);

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.is(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.id").exists());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.apps.length()", Matchers.is(3)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.preinstalledApps.length()", Matchers.is(5)));
    }

    @Test
    public void saveLink() throws Exception {

        LogicLink link = createLogicLink(logicTopology.getApps().get(0), logicTopology.getApps().get(2));


        String str = JSONUtil.serialize(link);


        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/logic_link/save");
        //希望服务器返回的格式
        builder.accept(MediaType.APPLICATION_JSON_UTF8);
        //发送数据格式
        builder.contentType(MediaType.APPLICATION_JSON_UTF8);
        builder.content(str);

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.is(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.id").exists());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.leftApp", Matchers.notNullValue()));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.rightApp", Matchers.notNullValue()));
    }


    @Test
    public void saveLink2() throws Exception {

        LogicLink link = createLogicLink(logicTopology.getApps().get(0), logicTopology.getApps().get(2));
        LogicLink link1 = createLogicLink(logicTopology.getApps().get(0), logicTopology.getApps().get(1));


        String str = JSONUtil.serialize(Arrays.asList(new LogicLink[]{link, link1}));


        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/logic_link/saves");
        //希望服务器返回的格式
        builder.accept(MediaType.APPLICATION_JSON_UTF8);
        //发送数据格式
        builder.contentType(MediaType.APPLICATION_JSON_UTF8);
        builder.content(str);

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.is(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()", Matchers.is(2)));
    }

    @Test
    public void query() throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/logic_topology/query");
        builder.param("params.id_eq", logicTopology.getId() + "");
        builder.accept(MediaType.APPLICATION_JSON_UTF8);

        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8));
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.is(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasSize(1)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].apps", Matchers.hasSize(3)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].preinstalledApps", Matchers.hasSize(5)));
    }
}
